//
//  DUHolidayController.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/11/18.
//

import Cocoa

class DUHolidayController: NSViewController, DUSystemProtocol {

    @IBOutlet weak var menuView: DUMenuView!
    @IBOutlet weak var mainView: NSView!
    private var manager = DUTypeManagerController()
    private var calpop = DUCalSelectedView()//DUCalSelectedController()
    @IBOutlet weak var dayView: DUCalDayButton!
    @IBOutlet weak var dayView2: DUCalDayButton!
    
    
    
    //private var calMenuItem = NSMenuItem.init()
    private var calMenu = NSMenu(title: "cal")
    //private var popover = NSPopover()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        GlobalControl.shared.system = self
        addChild(manager)
        mainView.addSubview(manager.view)
        createMenu(view: calpop)
        //calMenuItem.view = calpop
        //popover.contentViewController = calpop
        
    }
    
    func onChange(_ sender: DateEntity?) {
        calMenu.cancelTracking()
        
        
    }
    
    func enableAll(e: Bool) {
        menuView.isEnable = e
        manager.isEnable = e
    }
    
    @IBAction func testClick(_ sender: NSButton) {
        calpop.setDate(year: 2021, month: 11)
        calpop.multCalInHidden = false
        //print(sender.frame)
        let o = sender.frame.origin
        calMenu.popUp(positioning: calMenu.item(at: 0), at: NSPoint(x: o.x, y: o.y - 5), in: self.view)
        //NSMenu.popUpContextMenu(calMenu, with: NSApp.currentEvent!, for: sender)
        
        
        
    }
    
    private func createMenu (view: NSView) {
        let menuItem = NSMenuItem.init()
        menuItem.view = view
        calMenu.addItem(menuItem)
    }
    
    @IBAction func dayTestClick(_ sender: Any) {
        var d = CalendarUtil.getDateEntity(year: 2021, month: 12, day: 6)
        d.currentMonth = true
        d.dayType = .general
        dayView.state = .on
        dayView.setDate(date: d)
        
        var d1 = CalendarUtil.getDateEntity(year: 2021, month: 12, day: 6)
        d1.currentMonth = false
        d1.dayType = .general
        dayView2.setDate(date: d1)
        dayView2.setFestival()
    }
    
}
